package com.etc.zzysys.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.zzsys.entiy.AnswerBean;
import com.etc.zzsys.entiy.Question;
import com.etc.zzsys.service.QuestionService;
import com.etc.zzsys.service.impl.QuestionServiceImpl;
import com.etc.zzsys.util.BaseDao;
import com.etc.zzsys.util.PageData;

/**
 * Servlet implementation class QuestionServlet
 * 
 * 这个servlet处理的是所有和问题有关的请求
 */
@WebServlet("/qs.action")
public class QuestionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	// 创建QuestionService对象
	private QuestionService qs = new QuestionServiceImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public QuestionServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 处理请求编码:切记，处理请求第一件事情就是设置请求编码
		request.setCharacterEncoding("utf-8");
		// String v=request.getParameter("表单中的文本名，select名字等等/参数名"); 接收请求参数的值
		String op = request.getParameter("op");

		String req = "";
		if (request.getParameter("req") != null) {
			req = request.getParameter("req");
		}

		if ("queryquestion".equals(op)) {
			// 调用service的查询问题的方法...
			List<Question> list = qs.getQuestions();
			// list 我们需要将集合的数据传递页面 jsp
			// 最常用的方法是使用请求对象request对象来传递数据
			// request.setAttribute("string -key" ,Object value)
			System.out.println("queryquestion :" + list);
			request.setAttribute("list", list);
			// 从当前控制器跳转到jsp页面[问题列表]，跳转的方法叫做转发
			request.getRequestDispatcher("questions.jsp").forward(request, response);
		} else if ("queryquestion_jstl".equals(op)) {
			// 调用service的查询问题的方法...
			List<Question> list = qs.getQuestions();
			// PageData<AnswerBean> pd = BaseDao.getPage("select * from answer",
			// Integer.parseInt(request.getParameter("page")),
			// Integer.parseInt(request.getParameter("pageSize")), AnswerBean.class);
			// list 我们需要将集合的数据传递页面 jsp
			// 最常用的方法是使用请求对象request对象来传递数据
			// request.setAttribute("string -key" ,Object value)
			request.setAttribute("list", list);
			// request.setAttribute("pd", pd);
			// 从当前控制器跳转到jsp页面[问题列表]，跳转的方法叫做转发

			if ("front".equals(req)) {
				request.getRequestDispatcher("index-2.jsp").forward(request, response);
			} else {
				request.getRequestDispatcher("questions_jstl.jsp").forward(request, response);
			}
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
